// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
// import Vuex from 'vuex'
import store from './store/index'
import httpRequest from '@/utils/httpRequest' // api: https://github.com/axios/axios
import { isAuth } from '@/utils'
// import i18n from './I18n';
import VueI18n from 'vue-i18n';
import VueCookie from 'vue-cookie'
import less from 'less';
import '@/assets/fontIcon/iconfont.css'
import '@/assets/fontIcon/iconfont.js'
import axios from 'axios';

Vue.use(VueCookie)
Vue.use(ElementUI);
Vue.use(less);
Vue.use(VueI18n);
Vue.config.productionTip = false

// 挂载全局
Vue.prototype.$http = httpRequest // ajax请求方法
Vue.prototype.isAuth = isAuth     // 权限方法

const i18n = new VueI18n({
  locale: 'zh', // 设置默认语言
  messages: {}, // 初始时messages为空，之后动态添加
});

// let baseURL = "http://192.168.1.107:9999"
let baseURL = "https://kjdsapi.huike996.com"

function fetchLanguageData() {
  axios.get(baseURL + '/languageType/getLanguageTypeListMap')
    .then(res => {
      if (res.data.status == 0) {
        const languageList = res.data.data;
        localStorage.setItem('languageList', JSON.stringify(languageList));
        let language = localStorage.getItem('language')
        if (language == '' || language == null || language == undefined) {
          language = languageList[0].language
          localStorage.setItem('language', languageList[0].language);
        }
        // 假设默认选择第一个语言
        // const defaultLanguage = languageList[0].language;
        i18n.locale = language;
        // fetchLanguageFiles(defaultLanguage);
        fetchLanguageFiles(language);
      }
    });
}
function fetchLanguageFiles(language) {
  axios.get(baseURL + '/configI18n/getLanguageJsonList', { params: { language } })
    .then(res => {
      if (res.data.status == 0) {
        // 假设响应的数据格式是 { [language]: { ...translations... } }
        // console.log('aaaa',res)
        i18n.setLocaleMessage(language, res.data.data);
      }
    });
}
fetchLanguageData(); // 在应用启动时获取语言数据
// var language = localStorage.getItem('languageValue')
// // var languageList = JSON.parse(localStorage.getItem('languageList'))
// if(language == '' || language == null || language == undefined){
//   localStorage.setItem('languageValue','en')
//   getLanguageJsonList()
// }
// if (languageList == '' || languageList == null || languageList == undefined) {
//   // localStorage.setItem("languageValue", "en")
//   axios({
//     url: baseURL + '/languageType/getLanguageTypeListMap',
//     method: 'get',
//   }).then(res => {
//     if (res.status == 0) {
//       console.log(res, '国际化列表')
//       localStorage.setItem('languageList', res.data)
//     }
//   })
// }

// function getLanguageJsonList() {
//   axios({
//     url: baseURL + '/languageType/getLanguageTypeListMap',
//     method: 'get',
//   }).then(res => {
//     if (res.data.status == 0) {
//       console.log(res, '国际化列表')
//       localStorage.setItem('languageList', JSON.stringify(res.data.data))
//       getLanguageTypeListMap()
//     }
//   })
// }
// // console.log('axios',baseURL)
// function getLanguageTypeListMap() {
//   let dataObj = {
//     language: language
//   }
//   axios({
//     url: baseURL + '/configI18n/getLanguageJsonList',
//     method: 'get',
//     params: dataObj
//   }).then(res => {
//     console.log(res, 'aaaa----asda')
//   })
// }

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  i18n,
  components: { App },
  template: '<App/>'
})
